<?php

/*****************************************************

		NOT TESTED !
  
*****************************************************/

require_once 'db_base.inc';
require_once 'string.inc';


class Database 
	implements iDatabase
{
	function __construct($host, $user, $pass, $db) 
	{
		$this->conn = mysql_connect($host, $user, $pass);
		if (false === $this->conn) {
			throw Exception("mysql_connect error: " . mysql_error());
		}
			
		if (false === mysql_select_db($db, $this->conn)) {
			throw Exception("mysql_select_db error: " . mysql_error());
		}
	}
	
	function __destruct() { 
		mysql_close($this->conn); 
	}

	
	public function lastInsertId() 
	{
   	return mysql_insert_id($this->conn);
   }
   
   public function quote($s)
   {
		return mysql_real_escape_string($s, $this->conn);
   }
   
	public $conn = null;
};




class Query 
	implements iQuery, Iterator
{
	function __construct(Database $db) 
	{
		$this->db = $db;
	}
   
	public function prepare($sql)
	{
		$this->sql = $sql;
	}
	
	public function setParam($nm, $val) 
	{
		assert(!is_null($this->sql));  //no use to set param before calling prepare
		$snm = new String($nm);
		$this->params[$snm->prepend(':')] = $val;
		return $this;
	}
   
	public function clearParams() 
	{
		$this->params = array();
	}
   
	public function execute() 
	{
		assert(!is_null($this->sql));
		$this->close();
		
		$sql = new String($this->sql);
		foreach ($this->params as $nm => $val) {
			$sql->replace($nm, is_string($val) ? "'$val'" : $val);
		}
		
		$this->exec($sql);
   }
   
   public function exec($sql)
	{
		$this->close();
		
		$this->sql = $sql;
		$this->res = mysql_query($sql, $this->db->conn);
		if (false === $this->res) {
			throw new Exception("Invalid query [$sql] Error[".mysql_error($this->db->conn)."]"); 
		}
	}
	
	public function fetch($type=DB_FETCH_ASSOC)
	{
		if ($type==DB_FETCH_ASSOC) {
			$this->row = mysql_fetch_assoc($this->res);
		}
		else {
			$this->row = mysql_fetch_row($this->res);
		}
		return $this->row; 
	}
	
	public function restart() 
	{
		assert($this->res);
		mysql_data_seek($this->res, 0);
	}
	
	public function eof() 
	{
		return !$this->row;
	}
	
	public function at($fnm) 
	{
		assert(isset($this->res) && false !== $this->row);  //didn't call execute?
		
      if (!array_key_exists($fnm,$this->row)) 
      {
         throw new Exception(
            "Result column [$fnm] not found among [" . 
            implode(',',array_keys($this->row)) . "]"
         );
      }
      return $this->row[$fnm];
	}
	
	public function close()
	{
		$this->row = false;
		if (is_resource($this->res)) {
			mysql_free_result($this->res);
		}
		$this->res = false;
	}
	
	
	public function current() {
      return current($this->row);
   }

   public function key() {
      return key($this->row);
   }

   public function next() {
      return next($this->row);
   }

   public function valid() {
      return $this->current() !== false;
   }
   
   public function __toString() 
   {
      $s = '';
      foreach ($this->row as $key => $val) {
         $s .= "[$key] = $val\n";
      }
      return $s;
   }

   public function __clone() {
      assert(false);
   }
   
   

	protected $db = null;
	private $res = false;
	private $row = false;
	private $sql = null;
	private $params = array();
};


/** 
	were not implemented

	function goToRow($rowNum) {
		return mysql_data_seek($this->res,$rowNum);
	} 
	
	function affected() { 
		return mysql_affected_rows($this->db->conn); 
	}
	
	function countSelected() { 
		return mysql_num_rows($this->res);
	}
	
	function getEncoding() {
		return mysql_client_encoding($this->db->conn);
	}
*/

?>